package com.gxa.www.cl.mapper;

import com.gxa.www.cl.common.util.PurchaseDetail;
import com.gxa.www.cl.common.util.PurchaseDetail;
import com.gxa.www.cl.domain.vo.PurchaseOrders;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Mapper
public interface PurchaseDetailMapper {

    // 获取采购单信息
    @Select("SELECT * FROM 采购信息表")
    List<PurchaseDetail> getPurchaseDetails();

    // 插入采购明细
    @Insert("INSERT INTO purchase_details (purchaseOrderNumber, medicineCategory, medicineName, quantity, remarks) VALUES (#{purchaseOrderNumber}, #{medicineCategory}, #{medicineName}, #{quantity}, #{remarks})")
    int insertPurchaseDetail(PurchaseDetail purchaseDetail);

    // 获取药品类别
    @Select("SELECT DISTINCT medicineCategory FROM medicines")
    List<String> getMedicineCategories();

    // 根据类别获取药品
    @Select("SELECT name FROM medicines WHERE category = #{category}")
    List<String> getMedicinesByCategory(@Param("category") String category);


    @Select("SELECT \n" +
            "    c.danjuhao AS rukuId,\n" +
            "    c.shijian AS shijian,\n" +
            "    c.zongjiage AS zongjiage,\n" +
            "    c.jingshouren AS jingshouren,\n" +
            "    c.beizhu AS beizhu,\n" +
            "    g.catelog_id AS catelogId,\n" +
            "    g.name AS name,\n" +
            "    d.shuliang AS shuliang,\n" +
            "    g.beizhu AS beizhuyao,\n" +
            "    d.id AS id\n" +
            "FROM \n" +
            "    采购信息表 c\n" +
            "JOIN \n" +
            "    采购药品明细信息表 d ON c.id = d.ruku_id\n" +
            "JOIN \n" +
            "    药品信息表 g ON d.goods_id = g.id\n" +
            "JOIN \n" +
            "    药品分类表 cat ON g.catelog_id = cat.id;\n")
    List<PurchaseOrders> getPurchaseOrders();

    @Delete("DELETE FROM `采购药品明细信息表`\n" +
            "WHERE `id` = #{id};\n")
    void DeletePurchaseOrders(Integer id);

    @Update("UPDATE `采购药品明细信息表` d\n" +
            "JOIN `药品信息表` g ON d.goods_id = g.id\n" +
            "SET\n" +
            "    d.catelog_id = #{catelogId},\n" +
            "    g.name = #{name}, \n" +
            "    d.shuliang = #{shuliang},\n" +
            "    g.beizhu = #{beizhuyao} \n" +
            "WHERE d.id = #{id}; \n")
    void UpdatePurchaseOrders(PurchaseOrders purchaseOrders);
}
